Skip to content

Save the session incrementally instead of rewriting it every turn - #9

Merged
Darknode-Official merged 1 commit into
mainfrom
perf/session-delta-save
Nov 1, 2026
Merged

Darknode-Official merged 1 commit into
mainfrom
perf/session-delta-save

Conversation

@Darknode-Official

Copy link
Copy Markdown
Owner

Save the session incrementally instead of rewriting it every turn

Every end-of-turn save re-serialized and rewrote the whole session file
(34 MB on a long session even after #8). Now:

  • session.json is a full snapshot tagged with a generation id; each later
    save appends only the changed tail (first changed block onward) to
    .nexus/session.log.
  • Each block's saved JSON is cached until the block or one of its items
    changes (items carry a version, so a change that needs no redaction is
    still seen). Finding the changed tail is a reference compare.
  • The log is folded into a fresh snapshot once it outgrows half the
    snapshot, when most of the snapshot is dead (after /clear), on the
    first save of a process, or when another Nexus in the folder rewrote the
    snapshot. Age-based caps are re-applied only on those rewrites, so a
    block aging out of the recent window is not re-appended every turn.
  • One loader (boot auto-resume and /resume) replays the log; lines from
    another generation are skipped and a torn last line ends the replay.
    Old single-file session.json still loads unchanged.
  • session.log gets the same owner-only permissions as session.json.

On a copy of the long session: per-turn save 140 ms -> 15-20 ms, writing
~8 KB instead of 34 MB; load (snapshot + log) ~200 ms. The first save of
a process still writes a full snapshot (~1.1 s on that session).

Verified live: resume, two Ollama turns (both went to the log, snapshot
untouched), /restart restored both turns. test/session-save.test.js now
drives the real save/load code (deltas, edits to older blocks, ephemeral
blocks, torn/foreign log lines, compaction, aging, foreign rewrite,
legacy files).

Every end-of-turn save re-serialized and rewrote the whole session file
(34 MB on a long session even after #8). Now:

- session.json is a full snapshot tagged with a generation id; each later
  save appends only the changed tail (first changed block onward) to
  .nexus/session.log.
- Each block's saved JSON is cached until the block or one of its items
  changes (items carry a version, so a change that needs no redaction is
  still seen). Finding the changed tail is a reference compare.
- The log is folded into a fresh snapshot once it outgrows half the
  snapshot, when most of the snapshot is dead (after /clear), on the
  first save of a process, or when another Nexus in the folder rewrote the
  snapshot. Age-based caps are re-applied only on those rewrites, so a
  block aging out of the recent window is not re-appended every turn.
- One loader (boot auto-resume and /resume) replays the log; lines from
  another generation are skipped and a torn last line ends the replay.
  Old single-file session.json still loads unchanged.
- session.log gets the same owner-only permissions as session.json.

On a copy of the long session: per-turn save 140 ms -> 15-20 ms, writing
~8 KB instead of 34 MB; load (snapshot + log) ~200 ms. The first save of
a process still writes a full snapshot (~1.1 s on that session).

Verified live: resume, two Ollama turns (both went to the log, snapshot
untouched), /restart restored both turns. test/session-save.test.js now
drives the real save/load code (deltas, edits to older blocks, ephemeral
blocks, torn/foreign log lines, compaction, aging, foreign rewrite,
legacy files).
@Darknode-Official
Darknode-Official merged commit 7925a7f into main Nov 1, 2026
0 of 6 checks passed
@Darknode-Official
Darknode-Official deleted the perf/session-delta-save branch November 1, 2026 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant